package com.murphy.utils;

import com.google.gson.annotations.Expose;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 杨成雨
 * @create 2017-12-20 17:45
 */
public class PageController {
	@Expose
	private List<?> pageList; //结果集
	@Expose
	private int startRow; //查询起始行数
	@Expose
	private int currentPage; //当前页码（第几页）
	@Expose
	private int countsOfPerPage; //每页记录数
	@Expose
	private int totalCount; //记录总数（当前查询操作）
	private String orderColumn; //排序字段
	private String orderType; //排序方式（升、降）
	private Map<String, Object> srchField; //检索条件

	public PageController(int currentPage, int countsOfPerPage) {
		checkIfLargerThanZero(currentPage);

		this.currentPage = currentPage;
		this.countsOfPerPage = countsOfPerPage;
		this.startRow = 1;
	}

	public PageController(int currentPage, int perPagte, String orderColumn, String orderType) {
		this.countsOfPerPage = perPagte;
		
		startRow = 1;
		this.currentPage = currentPage;
		startRow = ((this.currentPage - 1) * countsOfPerPage) + 1;

		this.orderColumn = orderColumn;
		this.orderType = orderType;

		if (orderColumn == null)
			orderColumn = "ID";
		if (orderType == null)
			orderType = "DESC";
	}

	public PageController() {
		this.startRow = 1;
		this.currentPage = 1;
		countsOfPerPage = Integer.parseInt(PropertiesGetValue.getContextProperty( "default.counts.of.per.page"));
		orderColumn = "ID";
		orderType = "DESC";
	}

	//Getter & Setter
	public int getCountsOfPerPage() {
		return countsOfPerPage;
	}

	public void setCountsOfPerPage(int countsOfPerPage) {
		this.countsOfPerPage = countsOfPerPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
		this.startRow = ((this.currentPage - 1) * countsOfPerPage) + 1;
	}

	public String getOrderColumn() {
		return orderColumn;
	}

	public void setOrderColumn(String orderColumn) {
		this.orderColumn = orderColumn;
	}

	public String getOrderType() {
		return orderType;
	}

	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

	public List<?> getPageList() {
		return pageList;
	}

	public void setPageList(List<?> pageList) {
		if (pageList != null) {
			if (pageList.size() > countsOfPerPage) {
				pageList.remove(countsOfPerPage);
			}
		}

		this.pageList = pageList;
	}

	public Map<String, ?> getSrchField() {
		return srchField;
	}

	public void setSrchField(Map<String, Object> srchField) {
		this.srchField = srchField;
	}

	public int getStartRow() {
		return startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	//获取数据
	public void loadPage(PageDataMaker dataMaker) {
		pageList = dataMaker.getPageList(startRow, countsOfPerPage, orderColumn, orderType, srchField);
		totalCount = dataMaker.getTotalCount(srchField);

		if (pageList.size() > countsOfPerPage) {
			pageList.remove(countsOfPerPage);
		}
	}

	public void setRequestWithPageAttribute(HttpServletRequest request) {
		request.setAttribute("totalCnt", String.valueOf(getTotalCount()));
		request.setAttribute("curPage", String.valueOf(getCurrentPage()));
		request.setAttribute("perPage", String.valueOf(getCountsOfPerPage()));
		request.setAttribute("orderColumn", getOrderColumn());
		request.setAttribute("startRow", String.valueOf(startRow));
		request.setAttribute("orderType", getOrderType());
	}

	private void checkIfLargerThanZero(int startId) {
		if (startId < 1)
			throw new IllegalArgumentException("Start id must larger than zero");
	}

}